package com.mw.mwreaderdemo;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class S50_70 extends Activity {

	private Button btnOpenCard;
	private Button btnOpenCardLoop;
	private Button btnChkPwd;
	private Button btnReadCard;
	private Button btnWriteCard;
	private Button btnInitValue;
	private Button btnReadValue;
	private Button btnAddValue;
	private Button btnSubValue;
	private Button btnCloseCard;
	private Spinner spCardMode;
	private Spinner spPwdMode;
	private EditText etPwd;
	private EditText etSector;
	private EditText etBlock;
	private EditText etBlockData;
	private EditText etValue;
	private EditText result;
	private Thread mIdCardThread;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_s50_70);

		btnOpenCard = (Button) findViewById(R.id.btnOpenCard);
		btnOpenCardLoop = (Button) findViewById(R.id.btnOpenCardLoop);
		btnChkPwd = (Button) findViewById(R.id.btnChkPwd);
		btnReadCard = (Button) findViewById(R.id.btnReadCard);
		btnWriteCard = (Button) findViewById(R.id.btnWriteCard);
		btnInitValue = (Button) findViewById(R.id.btnInitValue);
		btnReadValue = (Button) findViewById(R.id.btnReadValue);
		btnAddValue = (Button) findViewById(R.id.btnAddValue);
		btnSubValue = (Button) findViewById(R.id.btnSubValue);
		btnCloseCard = (Button) findViewById(R.id.btnCloseCard);
		spCardMode = (Spinner) findViewById(R.id.spCardMode);
		spPwdMode = (Spinner) findViewById(R.id.spPwdMode);
		etPwd = (EditText) findViewById(R.id.etPwd);
		etSector = (EditText) findViewById(R.id.etSector);
		etBlock = (EditText) findViewById(R.id.etBlock);
		etBlockData = (EditText) findViewById(R.id.etBlockData);
		etValue = (EditText) findViewById(R.id.etValue);
		result = (EditText) findViewById(R.id.result);

		// 将输入字符转换为大写
		etBlockData.setTransformationMethod(new InputLowerToUpper());
		etPwd.setTransformationMethod(new InputLowerToUpper());

		//打开卡片
		btnOpenCard.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(S50_70.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					etResultAddStr("卡片序列号: " + MainActivity.reader.openCard(spCardMode.getSelectedItemPosition()));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});

		//循环打开卡片
		btnOpenCardLoop.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					mIdCardThread = new Thread() {
						@Override
						public void run() {
							onIdCardThreadRun();
						}
					};
					mIdCardThread.start();

				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});

		btnChkPwd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(S50_70.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					int sector = Integer.parseInt(etSector.getText().toString());
					MainActivity.reader.mifareAuth(spPwdMode.getSelectedItemPosition(), sector,
							etPwd.getText().toString());
					etResultAddStr("密码验证成功!");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});

		btnReadCard.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(S50_70.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					int sector = Integer.parseInt(etSector.getText().toString());
					int block = Integer.parseInt(etBlock.getText().toString());
					if (block / 4 != sector) {
						AlertDialog.Builder builder = new AlertDialog.Builder(S50_70.this);
						builder.setTitle("错误");
						builder.setMessage("您输入的块号不在扇区范围内!\n请重新输入。");
						builder.create();
						builder.show();
						return;
					}
					etResultAddStr(MainActivity.reader.mifareRead(block));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});

		btnWriteCard.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(S50_70.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					int sector = Integer.parseInt(etSector.getText().toString());
					int block = Integer.parseInt(etBlock.getText().toString());
					if (block / 4 != sector) {
						AlertDialog.Builder builder = new AlertDialog.Builder(S50_70.this);
						builder.setTitle("错误");
						builder.setMessage("您输入的块号不在扇区范围内!\n请重新输入。");
						builder.create();
						builder.show();
						return;
					}

					String strData = etBlockData.getText().toString();
					if (strData.length() != 32) {
						AlertDialog.Builder builder = new AlertDialog.Builder(S50_70.this);
						builder.setTitle("错误");
						builder.setMessage("您输入32位长度的数据。");
						builder.create();
						builder.show();
						return;
					}
					MainActivity.reader.mifareWrite(block, strData);
					etResultAddStr("写数据成功!");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});

		btnInitValue.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(S50_70.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					int sector = Integer.parseInt(etSector.getText().toString());
					int block = Integer.parseInt(etBlock.getText().toString());
					if (block / 4 != sector) {
						AlertDialog.Builder builder = new AlertDialog.Builder(S50_70.this);
						builder.setTitle("错误");
						builder.setMessage("您输入的块号不在扇区范围内!\n请重新输入。");
						builder.create();
						builder.show();
						return;
					}
					int value = Integer.parseInt(etValue.getText().toString());
					MainActivity.reader.mifareInitVal(block, value);
					etResultAddStr("初始化值 成功!");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});

		btnReadValue.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(S50_70.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					int sector = Integer.parseInt(etSector.getText().toString());
					int block = Integer.parseInt(etBlock.getText().toString());
					if (block / 4 != sector) {
						AlertDialog.Builder builder = new AlertDialog.Builder(S50_70.this);
						builder.setTitle("错误");
						builder.setMessage("您输入的块号不在扇区范围内!\n请重新输入。");
						builder.create();
						builder.show();
						return;
					}
					long value = MainActivity.reader.mifareReadVal(block);
					etResultAddStr("读值 成功!读出的值: " + value);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});

		btnAddValue.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(S50_70.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					int sector = Integer.parseInt(etSector.getText().toString());
					int block = Integer.parseInt(etBlock.getText().toString());
					if (block / 4 != sector) {
						AlertDialog.Builder builder = new AlertDialog.Builder(S50_70.this);
						builder.setTitle("错误");
						builder.setMessage("您输入的块号不在扇区范围内!\n请重新输入。");
						builder.create();
						builder.show();
						return;
					}
					int value = Integer.parseInt(etValue.getText().toString());
					MainActivity.reader.mifareIncrement(block, value);
					etResultAddStr("增值 成功! ");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});

		btnSubValue.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(S50_70.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					int sector = Integer.parseInt(etSector.getText().toString());
					int block = Integer.parseInt(etBlock.getText().toString());
					if (block / 4 != sector) {
						AlertDialog.Builder builder = new AlertDialog.Builder(S50_70.this);
						builder.setTitle("错误");
						builder.setMessage("您输入的块号不在扇区范围内!\n请重新输入。");
						builder.create();
						builder.show();
						return;
					}
					int value = Integer.parseInt(etValue.getText().toString());
					MainActivity.reader.mifareDecrement(block, value);
					etResultAddStr("减值 成功! ");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});

		btnCloseCard.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(S50_70.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					MainActivity.reader.halt();
					etResultAddStr("卡片已关闭! ");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});
	}

	private static final int MSG_ReadCardSuccess = 0;
	private static final int MSG_ReadCardFailes = 1;
	private static final int MSG_Compare = 2;
	private static final int socket_conn = 3;
	private static final int socket_send = 4;

	public String mNum;
	public int iNum = 0;
	private volatile boolean mIdCardThreadRun;
	private void onIdCardThreadRun() {
		mIdCardThreadRun = true;
		while (mIdCardThreadRun) {
			try {
				mNum = MainActivity.reader.openCard(spCardMode.getSelectedItemPosition());
				Thread.sleep(100);
//	        		MainActivity.reader.beep(1,2,1);
//	        		Thread.sleep(100);
//					MainActivity.reader.halt();
//					Thread.sleep(100);
				handler.sendEmptyMessage(MSG_Compare);
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private Handler handler = new Handler()
	{
		public void handleMessage(Message msg)
		{
			super.handleMessage(msg);
			switch (msg.what)
			{
				case MSG_ReadCardSuccess:

					break;
				case MSG_ReadCardFailes:

					break;

				case MSG_Compare:
					etResultAddStr("卡片序列号"+iNum+": " + mNum);
					iNum ++;
					break;
				case socket_conn:

					break;
				case socket_send: //socket发送

					break;
				default:
					break;
			}
		}
	};

	/*
	 * @Description 提示信息框添加信息
	 *
	 * @param str 要添加的信息
	 *
	 * @return void
	 */
	private void etResultAddStr(String str) {
		if(result.getLineCount() > 1000) //超过1000行，删除一次
		{
			result.setText("");
		}
		int index = result.getSelectionStart();
		Editable editable = result.getText();
		editable.insert(index, str+ "\n");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.s50_70, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
